package com.dctp.common.intercept;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

public class CorsInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        Controller controller = inv.getController();
        controller.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        controller.getResponse().setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
        controller.getResponse().setHeader("Access-Control-Allow-Headers", "*");
        
        // 对于OPTIONS预检请求，直接返回
        if ("OPTIONS".equalsIgnoreCase(controller.getRequest().getMethod())) {
            controller.renderNull();
            return;
        }
        inv.invoke();
    }
}
